/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controladores;

import controladores.exceptions.NonexistentEntityException;
import entidades.Tanque;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entidades.TipoCombustible;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Pablo
 */
public class TanqueJpaController implements Serializable {

    public TanqueJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Tanque tanque) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            TipoCombustible tipoCombustible = tanque.getTipoCombustible();
            if (tipoCombustible != null) {
                tipoCombustible = em.getReference(tipoCombustible.getClass(), tipoCombustible.getId());
                tanque.setTipoCombustible(tipoCombustible);
            }
            em.persist(tanque);
            if (tipoCombustible != null) {
                Tanque oldTanqueOfTipoCombustible = tipoCombustible.getTanque();
                if (oldTanqueOfTipoCombustible != null) {
                    oldTanqueOfTipoCombustible.setTipoCombustible(null);
                    oldTanqueOfTipoCombustible = em.merge(oldTanqueOfTipoCombustible);
                }
                tipoCombustible.setTanque(tanque);
                tipoCombustible = em.merge(tipoCombustible);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Tanque tanque) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Tanque persistentTanque = em.find(Tanque.class, tanque.getId());
            TipoCombustible tipoCombustibleOld = persistentTanque.getTipoCombustible();
            TipoCombustible tipoCombustibleNew = tanque.getTipoCombustible();
            if (tipoCombustibleNew != null) {
                tipoCombustibleNew = em.getReference(tipoCombustibleNew.getClass(), tipoCombustibleNew.getId());
                tanque.setTipoCombustible(tipoCombustibleNew);
            }
            tanque = em.merge(tanque);
            if (tipoCombustibleOld != null && !tipoCombustibleOld.equals(tipoCombustibleNew)) {
                tipoCombustibleOld.setTanque(null);
                tipoCombustibleOld = em.merge(tipoCombustibleOld);
            }
            if (tipoCombustibleNew != null && !tipoCombustibleNew.equals(tipoCombustibleOld)) {
                Tanque oldTanqueOfTipoCombustible = tipoCombustibleNew.getTanque();
                if (oldTanqueOfTipoCombustible != null) {
                    oldTanqueOfTipoCombustible.setTipoCombustible(null);
                    oldTanqueOfTipoCombustible = em.merge(oldTanqueOfTipoCombustible);
                }
                tipoCombustibleNew.setTanque(tanque);
                tipoCombustibleNew = em.merge(tipoCombustibleNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = tanque.getId();
                if (findTanque(id) == null) {
                    throw new NonexistentEntityException("The tanque with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Tanque tanque;
            try {
                tanque = em.getReference(Tanque.class, id);
                tanque.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The tanque with id " + id + " no longer exists.", enfe);
            }
            TipoCombustible tipoCombustible = tanque.getTipoCombustible();
            if (tipoCombustible != null) {
                tipoCombustible.setTanque(null);
                tipoCombustible = em.merge(tipoCombustible);
            }
            em.remove(tanque);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Tanque> findTanqueEntities() {
        return findTanqueEntities(true, -1, -1);
    }

    public List<Tanque> findTanqueEntities(int maxResults, int firstResult) {
        return findTanqueEntities(false, maxResults, firstResult);
    }

    private List<Tanque> findTanqueEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Tanque.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Tanque findTanque(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Tanque.class, id);
        } finally {
            em.close();
        }
    }

    public int getTanqueCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Tanque> rt = cq.from(Tanque.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
    public boolean yaExiste(String value, Long id_excepcion) {
        EntityManager em = getEntityManager();
        List<Tanque> result = null;
        try {
            String jpql = "SELECT p FROM Tanque p WHERE p.descripcion =:value";            
            Query query = em.createQuery(jpql);
            query.setParameter("value", value);
            result = query.getResultList();
            if(result.isEmpty())
                return false;
            else{
               for(Tanque v: result){
                   if(v.getId().equals(id_excepcion))
                       return false;
               }
               return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
}
